

/*
HappyFlags_2.pde - using flags in loop(). Polling flags with if(Timer.flagIsSet(0))

Copyright (c) 2010 Ed Bennett.  All rights reserved. 

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Polling flags with if(Timer.flagIsSet(0))is
// good for doing things that might take time to finish.
// It has the advantage that the timer system can run fast
// and the stuff in loop() can run slow. The speed of loop()
// depends on the number of flags that want servicing.
//
// Timer commands usually take a channel number and/or a 
// time-value. 
//
// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29

#include <TimerScheduler.h>

// hardware pins
int HF0 = 13;
int HF1 = 12;
int HF2 = 11;
int HF3 = 10;
int HF4 = 9;
int HF5 = 8;
int HF6 = 7;
int HF7 = 6;


int outval0 = 0;
int outval1 = 0;
int outval2 = 0;
int outval3 = 0;
int outval4 = 0;
int outval5 = 0;
int outval6 = 0;
int outval7 = 0;

int i;
int j;

void setup()
{   
 
  pinMode(HF0, OUTPUT); 
  pinMode(HF1, OUTPUT); 
  pinMode(HF2, OUTPUT);
  pinMode(HF3, OUTPUT); 
  pinMode(HF4, OUTPUT);
  pinMode(HF5, OUTPUT); 
  pinMode(HF6, OUTPUT);
  pinMode(HF7, OUTPUT); 

  Timer.begin();        // start the hardware timer

  Timer.repeat(0,1501); // (channel, time)
  Timer.repeat(1,770); 
  Timer.repeat(2,333);
  Timer.repeat(3,146); 
  Timer.repeat(4,65);
  Timer.repeat(5,111);
  Timer.repeat(6,222);
  Timer.repeat(7,333);

  
  Timer.start(0);       // (channel)
  Timer.start(1); 
  Timer.start(2); 
  Timer.start(3); 
  Timer.start(4); 
  Timer.start(5); 
  Timer.start(6); 
  Timer.start(7); 
  
  Serial.begin(1200);
}


void loop()
{
  // for HF0
  if(Timer.flagIsSet(0)){  // channel 0 flag
    outval0 ^= 1;          // toggle the pin
    digitalWrite(HF0, outval0);
  }

  // for HF1   
  if(Timer.flagIsSet(1)){  // channel 1 flag
    outval1 ^= 1;
    digitalWrite(HF1, outval1);
  }

  //for HF2  
  if(Timer.flagIsSet(2)){  // channel 2 flag
    outval2 ^= 1;
    digitalWrite(HF2, outval2);
  }
    //for HF3  
  if(Timer.flagIsSet(3)){  // channel 2 flag
    outval3 ^= 1;
    digitalWrite(HF3, outval3);
  }
    //for HF4  
  if(Timer.flagIsSet(4)){  // channel 2 flag
    outval4 ^= 1;
    digitalWrite(HF4, outval4);
    
  }
     //for HF5 
  if(Timer.flagIsSet(5)){  // channel 2 flag
    outval5 ^= 1;
    digitalWrite(HF5, outval5);
  }
    //for HF6 
  if(Timer.flagIsSet(6)){  // channel 2 flag
    outval6 ^= 1;
    digitalWrite(HF6, outval6);
  }
    //for HF7
  if(Timer.flagIsSet(7)){  // channel 2 flag
    outval7 ^= 1;
    digitalWrite(HF7, outval7);
  }
  
  i++;  
  Serial.print("fizzy foobar says \"We love you Thiiiiiis much! >  ");
  Serial.println(i);
  
}


